package com.fanchuan.uzx;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.fanchuan.uzx.ui.tab.FixedTabsAdapter;
import com.fanchuan.uzx.ui.tab.MainPageAdapter;
import com.fanchuan.uzx.ui.viewpaper.FixedTabsView;
import com.fanchuan.uzx.ui.viewpaper.TabsAdapter;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.UiThread;
import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.NotificationType;
import com.umeng.fb.UMFeedbackService;
import com.umeng.update.UmengUpdateAgent;

import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

@EActivity(R.layout.activity_main)
public class MainActivity extends SherlockFragmentActivity {
	private final static String TAG = "MainActivity";
	private ViewPager mPager;
	private PagerAdapter mPagerAdapter;
	private FixedTabsView mFixedTabs;
	private TabsAdapter mFixedTabsAdapter;
	
	public PopupWindow popWindow;

	private FragmentManager fManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		UmengUpdateAgent.update(this);
		UMFeedbackService.enableNewReplyNotification(this,
				NotificationType.AlertDialog);

		UMFeedbackService.setGoBackButtonVisible();
		fManager = getSupportFragmentManager();

	}

	
	@AfterViews
	void initView() {
		initTabsView();
	}
	
	public void initTabsView(){
		initViewPager(3, 0xFFFFFFFF, 0xFF000000);
		mFixedTabs = (FixedTabsView) findViewById(R.id.fixed_tabs);
		mFixedTabsAdapter = new FixedTabsAdapter(this);
		mFixedTabs.setAdapter(mFixedTabsAdapter, this);
		mFixedTabs.setViewPager(mPager);
		
		mFixedTabs.setTabClickListener(tabClickListener);

	}

	public View.OnClickListener tabClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			mFixedTabs.mPager.setCurrentItem(0);
			showPopWindow();
		}
	};

	private static final List<String> popListItems = new ArrayList<String>(
			Arrays.asList("Title and Icon","Submenu","Groups","Checkable","Shortcuts",
					"Order","Category","Visible","Disable"));
	
	public void showPopWindow() {
		
		if (popWindow == null) {
			popWindow = new PopupWindow(this);
			popWindow.setFocusable(true);
			ListView mListView = new ListView(this);

			mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item_icon_text, R.id.text, popListItems));
			mListView.setDivider(null);

			mListView.setVerticalScrollBarEnabled(false);
			LinearLayout layout = new LinearLayout(this);
			layout.setLayoutParams(new ViewGroup.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
			layout.setOrientation(LinearLayout.VERTICAL);
			
			mListView.setLayoutParams(new ViewGroup.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			layout.addView(mListView);
			
			popWindow.setContentView(layout);
			popWindow.setBackgroundDrawable(getResources().getDrawable(R.color.popup_window_bg));

			popWindow.setWidth(getWindowManager().getDefaultDisplay()
					.getWidth() / 3);
			popWindow.setHeight(LayoutParams.WRAP_CONTENT);
			
			mListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int position, long arg3) {
					Log.d(TAG, "position:>"+position);
					
					if(popWindow != null && popWindow.isShowing()){
						popWindow.dismiss();
						popWindow = null;
					}
				}
			});
		}
		popWindow.showAsDropDown(mFixedTabs);
	}

	private void initViewPager(int pageCount, int backgroundColor, int textColor) {
		mPager = (ViewPager) findViewById(R.id.pager);

		mPagerAdapter = new MainPageAdapter(fManager, pageCount, this);
		mPager.setAdapter(mPagerAdapter);
		mPager.setCurrentItem(1);
	}
	
	private long mLastBackClickedTime = -1;
	private static final long BACK_LEAVE_MARGIN = 1000 * 2;
	
	@Override
	public void onBackPressed() {
		long cur = System.currentTimeMillis();
		if(cur - mLastBackClickedTime > BACK_LEAVE_MARGIN){
			mLastBackClickedTime = cur;
			showToast(getString(R.string.back_exit_msg));
		}else {
			super.onBackPressed();
		}
	}

	@UiThread
	void showToast(String msg){
		Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	
}